extends Node2D
const PIPE_BOTTOM = preload("res://scenes/pipe_bottom.tscn")
@onready var ray_cast_2d = $RayCast2D
@export var ray_cast_spped:int = 30
var get_target:bool = false

func spawn_pipe_bottom(height):
	var pipe_bottom = PIPE_BOTTOM.instantiate()
	pipe_bottom.set_size(height)
	add_child(pipe_bottom)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if get_target:
		return
	if !ray_cast_2d.is_colliding() and !get_target:
		ray_cast_2d.target_position.y += ray_cast_spped
		ray_cast_2d.force_raycast_update()
	elif ray_cast_2d.is_colliding():
		var target = ray_cast_2d.get_collider()
		if target.is_in_group("ground"):
			get_target = true
			var target_height = ray_cast_2d.target_position.y # 目标距离发射点的高度
			spawn_pipe_bottom(target_height)
	pass
	
func _on_area_2d_body_entered(body):
	if body.is_in_group("player"):
		body.has_touch_pip = true
	pass # Replace with function body.


func _on_area_2d_body_exited(body):
	if body.is_in_group("player"):
		body.has_touch_pip = false
